local redis = require "resty.redis"
local commom = require "resty.common"
local log  = commom.hi_log

local pub = {}

--连接 redis server
function pub.open_redis(host, port, password)
    local red = redis.new()
    red:set_timeout(10000)
    local ok ,err = red:connect(host, port)
    if not ok then
        log("open redis error: "..err)
        return false
    end

    ok ,err = red:auth(password)
    if not ok then
        log("auth redis error: "..err)
        return false
    end

    return red
end

--断开 redis server 连接
function pub.close_redis(red)
    if red then
        local ok, err = red:set_keepalive(10000, 100)
        if not ok then
            log("set keepalive error: "..err)
            return false
        end
    end
end

return pub
